import java.awt.*;

/**
 * @author cocoa
 */
public class Card {

    //x 坐标
    private int x = 0;

    //y 坐标
    private int y = 0;

    //宽
    private final int w = 80;

    //高
    private final int h = 80;

    //下标 i
    private int i = 0;

    //下标 j
    private int j = 0;

    // 偏移量
    private final int start = 10;

    //数字
    private int num = 0;

    // 是否合并
    private boolean merge = false;

    // 计算坐标
    private void cal() {
        // +1 因为矩形框太靠左，不加1会贴着边
        this.x = this.start + this.j * this.w + (this.j + 1) * 5;
        this.y = this.start + this.i * this.h + (this.i + 1) * 5;
    }

    public Card(int i, int j) {
        this.i = i;
        this.j = j;

        this.cal();
    }


    public void draw(Graphics g) {
        // 根据数字获取对应的颜色
        Color color = this.getColor();
        Color oldColor = g.getColor();

        // 设置新颜色
        g.setColor(color);
        g.fillRoundRect(this.x, this.y, this.w, this.h, 4, 4);

        // 绘制数字
        if (this.num != 0) {
            g.setColor(new Color(125, 78, 51));
            Font font = new Font("思源宋体", Font.BOLD, 30);
            g.setFont(font);
            String text = String.valueOf(this.num);
            int textLen = getWordWidth(font, text, g);
            int tx = this.x + (this.w - textLen) / 2;
            int ty = this.y + 50;
            g.drawString(text, tx, ty);
        }

        // 还原颜色
        g.setColor(oldColor);
    }

    // 获取颜色
    private Color getColor() {
        Color color = null;
        switch (this.num) {
            case 2:
                color = new Color(238, 244, 234);
                break;
            case 4:
                color = new Color(222, 236, 200);
                break;
            case 8:
                color = new Color(174, 213, 130);
                break;
            case 16:
                color = new Color(142, 201, 75);
                break;
            case 32:
                color = new Color(111, 148, 48);
                break;
            case 64:
                color = new Color(76, 174, 124);
                break;
            case 128:
                color = new Color(60, 180, 144);
                break;
            case 256:
                color = new Color(45, 130, 128);
                break;
            case 512:
                color = new Color(9, 97, 26);
                break;
            case 1024:
                color = new Color(242, 177, 121);
                break;
            case 2048:
                color = new Color(223, 105, 0);
                break;
            default:
                color = new Color(92, 151, 117);
                break;
        }

        return color;
    }

    /**
     * 根据字体和内容获取字符串的长度
     * @param font
     * @param content
     * @param g
     * @return
     */
    public static int getWordWidth(Font font, String content, Graphics g) {
        // 获取当前字体的字体度量,用于计算文字长度
        FontMetrics metrics = g.getFontMetrics(font);
        int width = 0;
        for (int i = 0; i < content.length(); i++) {
            // 获取每个字符的宽度，累加起来
            width += metrics.charWidth(content.charAt(i));
        }

        return width;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public int getNum() {
        return this.num;
    }

    public boolean isMerge() {
        return this.merge;
    }

    public void setMerge(boolean merge) {
        this.merge = merge;
    }

    // 向上移动
    public void moveTop(Card[][] cards) {
        // 设定递归退出条件
        if (this.i == 0) {
            return;
        }
        // 取得上一个卡片
        Card prev = cards[this.i - 1][this.j];

        if (prev.getNum() == 0) {
            prev.setNum(this.num);
            this.num = 0;
            // 递归上一个卡片，如果递归的是当前卡片只能移动一格
            prev.moveTop(cards);
        } else if (prev.getNum() == this.num && !prev.merge) {
            // 合并
            prev.merge = true;
            prev.setNum(prev.getNum() * 2);
            this.num = 0;
        }

    }

    public void moveRight(Card[][] cards) {
        // 设定递归退出条件
        if (this.j == 3) {
            return;
        }
        // 取得上一个卡片
        Card prev = cards[this.i][this.j + 1];

        if (prev.getNum() == 0) {
            prev.setNum(this.num);
            this.num = 0;
            // 递归上一个卡片，如果递归的是当前卡片只能移动一格
            prev.moveRight(cards);
        } else if (prev.getNum() == this.num && !prev.merge) {
            // 合并
            prev.merge = true;
            prev.setNum(prev.getNum() * 2);
            this.num = 0;
        }
    }

    public void moveDown(Card[][] cards) {
        // 设定递归退出条件
        if (this.i == 3) {
            return;
        }
        // 取得上一个卡片
        Card prev = cards[this.i + 1][this.j];

        if (prev.getNum() == 0) {
            prev.setNum(this.num);
            this.num = 0;
            // 递归上一个卡片，如果递归的是当前卡片只能移动一格
            prev.moveDown(cards);
        } else if (prev.getNum() == this.num && !prev.merge) {
            // 合并
            prev.merge = true;
            prev.setNum(prev.getNum() * 2);
            this.num = 0;
        }
    }

    public void moveLeft(Card[][] cards) {
        // 设定递归退出条件
        if (this.j == 0) {
            return;
        }
        // 取得上一个卡片
        Card prev = cards[this.i][this.j - 1];

        if (prev.getNum() == 0) {
            prev.setNum(this.num);
            this.num = 0;
            // 递归上一个卡片，如果递归的是当前卡片只能移动一格
            prev.moveLeft(cards);
        } else if (prev.getNum() == this.num && !prev.merge) {
            // 合并
            prev.merge = true;
            prev.setNum(prev.getNum() * 2);
            this.num = 0;
        }
    }
}
